package com.example.demo.common;

import com.example.demo.common.constant.ResultCode;
import com.example.demo.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

@Slf4j
public class Valid {
    // 判断是否为空
    public static boolean isEmpty(String value) {
        return value == null || value.trim().isEmpty();
    }

    // 判断是否Base64字符串
    public static boolean isBase64(String value) {
        return Pattern.matches("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$", value);
    }

    // 验证手机号码
    public static boolean isMobile(String value) {
        String reg = "((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|" +
                "(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))";
        return Pattern.matches(reg, value);
    }

    // 验证座机号码
    public static boolean isPhone(String value) {
        return Pattern.matches("^[0][1-9]{2,3}-[0-9]{5,10}$", value) || Pattern.matches("^[1-9]{1}[0-9]{5,8}$", value);
    }

    // 验证手机号码或座机号码
    public static boolean isContact(String value) {
        boolean res1 = Pattern.matches("^[0][1-9]{2,3}-[0-9]{5,10}$", value);
        boolean res2 = Pattern.matches("^[1-9]{1}[0-9]{5,8}$", value);

        String reg = "((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|" +
                "(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))";
        boolean res3 = Pattern.matches(reg, value);
        return res1 || res2 || res3;
    }

    // 验证邮箱
    public static boolean isEmail(String value) {
        return Pattern.matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$", value);
    }

    // 判断是否为金额
    public static boolean isMoney(String value) {
        return Pattern.matches("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$", value);
    }

    // 判断是否为日期
    public static boolean isDate(String value) {
        return Pattern.matches("((19|20)[0-9]{2})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])", value);
    }

    // 判断是否为正整数与0
    public static boolean isNum(String value) {
        return Pattern.matches("^[1-9]\\d*|0$", value);
    }

    // 判断是否为网址
    public static boolean isUrl(String value) {
        return Pattern.matches("(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?", value);
    }

    // 判断数值范围
    public static boolean isRange(String value, int min, int max) {
        if (!Valid.isNum(value) && !Valid.isMoney(value)) {
            return false;
        }
        if (min >= max) {
            return false;
        }

        double temp = Double.parseDouble(value);
        return temp >= min && temp <= max;
    }

    // 判断字符串长度
    public static boolean isLength(String value, int min, int max) {
        if (min > max) {
            return false;
        }

        int length = value.length();
        return length >= min && length <= max;
    }

    // 判断特殊字符
    public static boolean isWicked(String value) {
        return Pattern.matches(".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\\\\]+.*", value);
    }

    // 判断SQL注入
    public static boolean isSqlInject(String value) {
        // 也可以使用MyBatis-Plus自带的方法
        // SqlInjectionUtils.check(value.toLowerCase());

        String[] keywordList = {"insert", "select", "update", "delete", "and", "or", "from", "where"};
        StringBuilder pattern = new StringBuilder();
        for (String keyword : keywordList) {
            if (pattern.length() > 0) {
                pattern.append("|"); // 使用 "|" 作为分隔符来构建 OR 条件
            }
            pattern.append("\\b").append(Pattern.quote(keyword)).append("\\b"); // 添加单词边界和转义特殊字符
        }
        return Pattern.compile(pattern.toString()).matcher(value.toLowerCase()).find();
    }

    // 判断字符串是否为null或空，若是则抛出数据异常
    public static void StringNotEmpty(String value, String tip) {
        if (isEmpty(value)) {
            throw new DataException(tip);
        }
    }

    public static void StringNotEmpty(String value, ResultCode resultCode) {
        if (isEmpty(value)) {
            throw new DataException(resultCode);
        }
    }

    // 判断字符串内容是否与给定内容相等，若否则抛出数据异常
    public static void StringIsEquals(String value, String data, String tip) {
        if (!value.equals(data)) {
            throw new DataException(tip);
        }
    }

    // 判断字符串内容是否与给定数组中的内容相等，若否则抛出数据异常
    public static void StringIsEquals(String value, String[] dataArray, String tip) {
        List<String> dataList = Arrays.asList(dataArray);
        if (!dataList.contains(value)) {
            throw new DataException(tip);
        }
    }

    // 判断对象是否为null，若是则抛出数据异常
    public static void ObjectNotNull(Object obj, String tip) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new DataException(tip);
        }
    }

    public static void ObjectNotNull(Object obj, ResultCode resultCode) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new DataException(resultCode);
        }
    }

}
